<script setup>
import { ref } from 'vue';
import http from "../common/http";
import { getToken, clearToken } from '../store';
import { useRouter } from 'vue-router';

const router = useRouter();
const userInfo = ref({});

function logout() {
    clearToken();
    router.push("/login");
}

if (!getToken()) {
    router.push("/login");
}

http.get("main/user_info").then(res => {
    userInfo.value = res.data.data;
});
</script>

<template>
    <div class="index-container">
        <header class="header">
            <div class="header-left">
                仓库收货系统
            </div>
            <div class="user-info">
                <i class="fas fa-user"></i>
                <span>欢迎，{{ userInfo.username }}</span>
                <button class="logout-btn" @click="logout">
                    <i class="fas fa-sign-out-alt"></i>
                    退出
                </button>
            </div>
        </header>

        <div class="main-content">
            <div class="menu-grid">
                <div class="menu-item" @click="router.push('/receive')">
                    <h3>入库扫描</h3>
                    <p>物品扫描换单</p>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.index-container {
    width: 100%;
    min-height: 100vh;
    font-family: 'Arial', sans-serif;
}

.header {
    background-color: #fff;
    padding: 1rem 2rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #515050;
}

.header-left {
    font-size: 1.5rem;
    font-weight: bold;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.logout-btn {
    background-color: #ff4444;
    color: white;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.logout-btn:hover {
    background-color: #ff6666;
}

.main-content {
    padding: 2rem;
}

.menu-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 2rem;
    padding: 1rem;
}

.menu-item {
    background-color: #fff;
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: transform 0.2s;
}

.menu-item:hover {
    transform: translateY(-5px);
}

.menu-item h3 {
    margin: 0 0 1rem 0;
    color: #333;
}

.menu-item p {
    margin: 0;
    color: #666;
}

@media (prefers-color-scheme: dark) {
    .header {
        background-color: #1a1a1a;
        color: #fff;
    }

    .menu-item {
        background-color: #1a1a1a;
    }

    .menu-item h3 {
        color: #fff;
    }

    .menu-item p {
        color: #ccc;
    }
}
</style>